package com.asa.ui.I;

import java.awt.EventQueue;
import java.util.Arrays;

import com.asa.ui.I.AlgoFrame;
import com.asa.ui.I.sort.SelectionSortData;
import com.asa.ui.frame.AlgoVisHelper;


public class AlgoVisualizer {
	private AlgoFrame frame;
	private SelectionSortData data;
	public AlgoVisualizer(final int screenWidth,final int screenHeight,int N){
		
		data = new SelectionSortData(N, screenHeight);
		
		EventQueue.invokeLater(new Runnable() {//用java事件分发（开了个线程）的方法来创建对话框
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				frame=new AlgoFrame("随机分钱开始问题模拟",screenWidth,screenHeight,data);
				
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						runmiage();
					}

					
				}).start(); ;
				
			}
		});
		
		
	}
	//播放动画
	public void runmiage(){
		
			setData(0, -1, -1);
			
			for (int i = 0; i < data.N(); i++) {
				int minIndex = i;
				setData(i, -1, minIndex);

				for (int j = i+1; j < data.N(); j++) {
					setData(i, j, minIndex);
					if (data.get(j)<data.get(minIndex)) {
						minIndex = j;
						setData(i, j, minIndex);

					}
				}
				data.swap(i, minIndex);
				setData(i+1, -1, -1);


			}
			
			setData(data.N(), -1, -1);

		
	}

	private void setData(int orderedIndex,int currentMinIndex,int currentCompareIndex) {
		data.orderedIndex = orderedIndex;
		data.currentMinIndex = currentMinIndex;
		data.currentCompareIndex = currentCompareIndex;

		frame.render(data);
		AlgoVisHelper.pause(40);
	}
	
	
}